package zrar;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import zrar.entity.AnswerPeople;
import zrar.util.ExcelUtil;
import zrar.util.SystemUtil;



public class InputDataFrame extends JFrame{
			
	private static final long serialVersionUID = 1L;
	
	/**top**/
	private JPanel topPane;
	private JLabel file_lb;
	private JTextField file_txt;
	private JButton file_but;
	private JFileChooser fileChooser;
	/**top**/
	
	/**middle**/
	private JPanel middlePane;
	private JLabel people_lb;
	private JList list;
	private PeopleModelList mode;
	private JScrollPane scrollPane;
	private JButton all_but;
	private JButton del_but;
	/**middle**/
	
	/**bottom**/
	private JPanel bottomPane;
	private JButton save_but;
	private JButton cancel_but;
	/**bottom**/
	
	static final int WIDTH = 480;
	static final int HEIGHT = 500;
	private int position_x=30;	
	private int position_y=20;
	
	private List<AnswerPeople> sys_peoples = new ArrayList<AnswerPeople>();
	private List<AnswerPeople> local_peoples = new ArrayList<AnswerPeople>();
	
	public  InputDataFrame(){
		this.setTitle("导入数据");
		
		//顶部
		topPane = new JPanel();	
		FlowLayout flow1 = new FlowLayout(FlowLayout.CENTER,15,5);
		topPane.setLayout(flow1);
		file_lb = new JLabel("导入文件");
		file_txt = new JTextField(25);
		file_but = new JButton("浏览...");
		topPane.add(file_lb);
		topPane.add(file_txt);
		topPane.add(file_but);
		
		fileChooser = new JFileChooser("D:\\");
		
		//中间
		middlePane = new JPanel();
		middlePane.setLayout(null);
		people_lb = new JLabel("候选人名单");
		mode = new PeopleModelList();
		list = new JList(mode);
		scrollPane = new JScrollPane(list);
		all_but = new JButton("全选");
		del_but = new JButton("删除");
		
		middlePane.add(people_lb);
		middlePane.add(scrollPane);
		middlePane.add(all_but);
		middlePane.add(del_but);
		
		
		//底部
		bottomPane = new JPanel();
		FlowLayout flow = new FlowLayout(FlowLayout.CENTER,60,5);
		bottomPane.setLayout(flow);
		save_but = new JButton("保存");
		cancel_but = new JButton("取消");
		
		bottomPane.add(save_but);
		bottomPane.add(cancel_but);
		
		init();
		this.add(topPane);
		this.add(middlePane);
		this.add(bottomPane);
		this.setSize(WIDTH, HEIGHT);
		SystemUtil.showInCenter(this);
		//this.setVisible(true);
	}
	
	private void init(){
		this.setLayout(null);
		
		this.getContentPane().setBackground(Color.WHITE);
		
		topPane.setBounds(position_x,position_y, 400,40 );		
		middlePane.setBounds(position_x,position_y+60, 400,290);		
		bottomPane.setBounds(position_x,position_y+380, 400,40 );
		
		people_lb.setBounds(5,5,100,25);
		scrollPane.setBounds(5,35,380,200);
		all_but.setBounds(200,245,60,25);
		del_but.setBounds(300,245,60,25);
		
		file_but.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				// 导入抽奖人员数据源
				DataSource_Action(e);
			}
		});
		
		del_but.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				// 删除抽奖人员
				delete_Action(e);
			}
		});
		
		all_but.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				// 全选				
				allselect_Action(e);				
			}
		});
		save_but.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				
				if(sys_peoples.size()!=0){
					sys_peoples.clear();
				}
				
				sys_peoples.addAll(local_peoples);
				
				SystemUtil.setPeoplelist(sys_peoples);
				hideFrame();
			}
		});
		cancel_but.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				hideFrame();
			}
		});
	}
	
	public void hideFrame(){
		file_txt.setText("");
		this.setVisible(false);
	}
	public void showFrame(){
		local_peoples.clear();
		sys_peoples = SystemUtil.getPeoplelist();
		local_peoples.addAll(sys_peoples);
		
		mode = new PeopleModelList();
		list.setModel(mode);
		this.setVisible(true);
	}
	
	protected void allselect_Action(MouseEvent e) {
		int size=mode.getSize();
		if(size>0){
			int[] indexs = new int[size];
			indexs[0]=size;
			for(int i=0;i<size;i++){
				indexs[i]=i;
			}
			list.setSelectedIndices(indexs);
		}
	}

	public void delete_Action(MouseEvent e) {

		int[] index = list.getSelectedIndices();
		int endindex = index.length - 1;
		for (int i = endindex; i >= 0; i--) {
			//System.out.println("选中的====" + index[i]);
			mode.removeElementAt(index[i]);
			local_peoples.remove(index[i]);
		}

	}

	protected void DataSource_Action(MouseEvent e) {
		fileChooser.setApproveButtonText("确定");
		fileChooser.setDialogTitle("打开文件");
		File file = null;
		int result = fileChooser.showOpenDialog(this);

		if (result == JFileChooser.APPROVE_OPTION) {
			file = fileChooser.getSelectedFile();
			//System.out.println("您导入的文件：" + file.getName());
			file_txt.setText(file.getPath());
			if(!SystemUtil.checkFileExtendName(file.getName(), "xls")){
				JOptionPane.showMessageDialog(null, "请选择excel文件");
				return;
			}
		} else if (result == JFileChooser.CANCEL_OPTION) {
			//System.out.println("您没有导入任何文件");
		}

		if (file != null) {
			List<AnswerPeople> addpeoples = ExcelUtil.readExcelFile(file);
			local_peoples.addAll(addpeoples);
			

			if (local_peoples.size() > 1) {
				mode = new PeopleModelList();
				list.setModel(mode);
				//list.setFont(new Font("楷体",Font.PLAIN,20));
			} else {
				//System.out.println("没有读取到excel的内容");
			}
		}
	}
	
	public static void main(String[] args) {
		new InputDataFrame();
		
	}

	
	class PeopleModelList extends DefaultListModel// 创建一个继承了DefaultListModel类的类
	{
		private static final long serialVersionUID = 1L;

		PeopleModelList() {
			for (int i = 0; i < local_peoples.size(); i++)
				addElement((i + 1) + "." + local_peoples.get(i).getPeopleName());
		}

	}
}
